﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DHQGHN.Common;
using DHQGHN.DAL.Common;
using DHQGHN.PM0.Models.LDVL;
using DHQGHN.PM0.Controllers;
using DHQGHN.DAL.BO.LDVL;
using DHQGHN.PM0.Common.Attributes;

namespace DHQGHN.PM0.Controllers
{
    public class ContractReasonController : BaseController
    {
        public const int REQUEST_ACTIVE = 1;
        ContractReasonBO ContReasonBO;

        public ActionResult Index(ContractReasonModel a = null, int pageIndex = 0, string sort = "Id", string sortDir = "ASC", string curentID = "")
        {
            ViewBag.ActiveMenus = ViewBag.ActiveMenus == null ? new System.Collections.Hashtable() : ViewBag.ActiveMenus;
            ViewBag.ActiveMenus[13] = "active";
            ViewBag.ActiveMenus[249] = "active";
            a.DELETED = 2;
            //a.Levels = 0;

            var model = Search(a, pageIndex, curentID, false);
            return View(model);
        }

        [HttpPost]
        public ActionResult ActionSearch(ContractReasonModel a = null, int pageIndex = 0, string sort = "Id", string sortDir = "ASC", string curentID = "")
        {
            //a.DELETED = 2;
            var model = Search(a, pageIndex, curentID, false);
            return PartialView("~/Views/ContractReason/_ContractReasonList.cshtml", model);
        }

        private DataPage<ContractReasonModel> Search(ContractReasonModel aModel, int pageIndex, string currentId, bool compareEqual)
        {
            ContReasonBO = new ContractReasonBO();
            var DALModel = ContReasonBO.GetDynamicListPaging(aModel.toDALModel(), PageSize, pageIndex, compareEqual);
            var models = new DataPage<ContractReasonModel>();
            var lstItem = DALModel.Data;
            foreach (DHQGHN.DAL.Models.LDVL.ContractReason area in lstItem)
            {
                models.Data.Add(new ContractReasonModel(area));
            }
            ContractReasonModel model = null;
            if (!String.IsNullOrEmpty(currentId) && models.Data.Count > 0)
            {
                model = models.Data.Find(user => Equals(user.LS_CONTRACT_REASON_ID, currentId));
                models.CustomData = model;
            }
            else
            {
                models.CustomData = aModel;
            }
            if (models.Data.Count > 0)
            {
                //Tao component phan trang
                var pagNative = new Pagination
                {
                    ActionName = "actionSearch",
                    ModelName = "ContractReason",
                    CurrentPage = pageIndex,
                    InputSearchId = "",
                    TagetReLoadId = "ContractReason-list",

                };
                pagNative.TotalRows = models.Data[0].RECORD_COUNT;
                pagNative.CurrentRow = models.Data.Count;
                ViewBag.Pagination = pagNative;
            }
            return models;
        }

        public ActionResult CreatePopupUpdate(string id)
        {
            ContractReasonModel model = null;
            if (!(String.IsNullOrWhiteSpace(id) && String.IsNullOrEmpty(id)))
            {
                ContReasonBO = new ContractReasonBO();
                model = new ContractReasonModel(ContReasonBO.GetById(id));
            }
            if (model == null)
            {
                model = new ContractReasonModel();
            }
            if (Request.IsAjaxRequest())
            {
                return PartialView(model);
            }
            return View(model);
        }

        /// Thêm mới loại đơn vị hành chính
        [HttpPost]
        [ValidateInput(false)]
        [AjaxValidateAntiForgeryToken]
        public ActionResult ActionSave(ContractReasonModel model)
        {
            if (Request.IsAjaxRequest())
            {
                ContReasonBO = new ContractReasonBO();
                bool result = ContReasonBO.Save(model.toDALModel());
                if (result)
                {
                    if (!(String.IsNullOrEmpty(model.LS_CONTRACT_REASON_ID) || String.IsNullOrWhiteSpace(model.LS_CONTRACT_REASON_ID)))
                    {
                        return Json(JsonResponse.Json200("Cập nhật thành công!"));
                    }
                    else
                    {
                        return Json(JsonResponse.Json200("Thêm mới thành công!"));
                    }
                }
                else
                {
                    return Json(JsonResponse.Json500("Xảy ra lỗi trong quá trình xử lý!"));
                }
            }
            return null;
        }

        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult ActionDelete(string[] listID)
        {
            if (Request.IsAjaxRequest())
            {
                ContReasonBO = new ContractReasonBO();
                bool result = ContReasonBO.Delete(listID);

                if (result)
                {
                    //Tra ve ket qua thanh cong
                    return Json(JsonResponse.Json200(Resources.Localization.Confirm_Unuse_Success));
                }
                else
                {
                    //Tra ve ket qua loi
                    return Json(JsonResponse.Json500(Resources.Localization.Confirm_Save_Error));
                }
            }
            return null;
        }

        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult ActionActive(string id)
        {
            if (Request.IsAjaxRequest())
            {
                ContReasonBO = new ContractReasonBO();
                bool result = ContReasonBO.Active(id, REQUEST_ACTIVE);
                if (result)
                {
                    return Json(JsonResponse.Json200("Khôi phục thành công!"));
                }
                else
                {
                    return Json(JsonResponse.Json500("Xảy ra lỗi trong quá trình xử lý!"));
                }
            }
            return null;
        }

        public JsonResult CheckExistName(string NAME, string LS_CONTRACT_REASON_ID)
        {
            ContractReasonModel myModel = new ContractReasonModel()
            {
                NAME = NAME.Trim(),
                DELETED = 2
            };

            if (IsExistName(myModel, LS_CONTRACT_REASON_ID))
            {
                return Json("Tên này đã tồn tại");
            }
            else
            {
                return Json(true, JsonRequestBehavior.AllowGet);
            }
        }

        public JsonResult CheckExistCode(string CODE, string LS_CONTRACT_REASON_ID)
        {
            ContractReasonModel myModel = new ContractReasonModel()
            {
                CODE = CODE,
                DELETED = 2
            };
            if (IsExistCode(myModel, LS_CONTRACT_REASON_ID))
            {
                return Json("Mã này đã tồn tại");
            }
            else
            {
                return Json(true, JsonRequestBehavior.AllowGet);
            }
        }

        private bool IsExistCode(ContractReasonModel model, string id)
        {
            var dataPaging = Search(model, 0, "", true);
            if (dataPaging.Data.Any())
            {
                var lstItem = dataPaging.Data;
                foreach (ContractReasonModel item in lstItem)
                {
                    if (!(String.IsNullOrWhiteSpace(id) || String.IsNullOrEmpty(id)))
                    {
                        if (!(item.LS_CONTRACT_REASON_ID.ToString().Equals(id)) &&
                            (model.CODE == item.CODE))
                        {
                            return true;
                        }
                    }
                    else
                    {
                        //Kiem tra truong hop them moi
                        return true;
                    }
                }
            }
            return false;
        }

        private bool IsExistName(ContractReasonModel model, string id)
        {
            var dataPaging = Search(model, 0, "", true);
            if (dataPaging.Data.Any())
            {
                var lstItem = dataPaging.Data;
                foreach (ContractReasonModel item in lstItem)
                {
                    if (!(String.IsNullOrWhiteSpace(id) || String.IsNullOrEmpty(id)))
                    {
                        if (!(item.LS_CONTRACT_REASON_ID.ToString().Equals(id)) &&
                            (model.NAME == item.NAME))
                        {
                            return true;
                        }
                    }
                    else
                    {
                        //Kiem tra truong hop them moi
                        return true;
                    }
                }
            }
            return false;
        }

	}
}